# -*- coding: utf-8 -*-
"""
-------------------------------------------------
  File Name：   My_TextEdit.py
  Description :
  Author :    崔术森
  Eamil  :    deer_cui@163.com
  date：     2021/11/15
-------------------------------------------------
  Change Activity:
          2021/11/15:
-------------------------------------------------
"""
__author__ = '崔术森'

from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt


class My_QTextEdit(QtWidgets.QTextEdit):
    # 自定义e信号, 注意信号必须为类属性
    keypress = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(My_QTextEdit, self).__init__(parent)
        self.shift_ispress = False

    def keyPressEvent(self, e: QtGui.QKeyEvent) -> None:
        QtWidgets.QTextEdit.keyPressEvent(self, e)
        # print(e.text())
        if e.key() == Qt.Key_Shift:
            self.shift_ispress = True
            # print("按压shift")
        elif (e.key() == Qt.Key_Enter or e.key() == Qt.Key_Return) and self.shift_ispress:
            cursor = self.textCursor()
            cursor.clearSelection()
            cursor.deletePreviousChar()
            # print("shift+enter")
            self.insertPlainText("\n")
        elif e.key() == Qt.Key_Enter or e.key() == Qt.Key_Return:
            cursor = self.textCursor()
            cursor.clearSelection()
            cursor.deletePreviousChar()
            self.keypress.emit()
            # print("按了回车")

    def keyReleaseEvent(self, e: QtGui.QKeyEvent) -> None:
        if e.key() == Qt.Key_Shift:
            self.shift_ispress = False
            # print("释放shift键盘")

    # 可在外部与槽函数连接
    def connect_customized_slot(self, func):
        self.keypress.connect(func)


class My_TextEdit_Show(QtWidgets.QTextEdit):
    # 自定义e信号, 注意信号必须为类属性
    doubleClick = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(My_TextEdit_Show, self).__init__(parent)

    # def mouseDoubleClickEvent(self, e: QtGui.QMouseEvent) -> None:
    #     self.doubleClick.emit()

    # 可在外部与槽函数连接
    def connect_customized_slot(self, func):
        self.doubleClick.connect(func)